package com.caocaog.market.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.finance.entity.MkBillChange;
import com.caocaog.market.finance.entity.MkBillLine;
import com.caocaog.market.finance.mapper.MkBillChangeMapper;
import com.caocaog.market.finance.mapper.MkBillLineMapper;
import com.caocaog.market.finance.service.IMkBillChangeService;
import com.caocaog.market.finance.vo.MkBillLineVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MkBillChangeImpl extends ServiceImpl<MkBillChangeMapper, MkBillChange> implements IMkBillChangeService {

    @Resource
    MkBillLineMapper mkBillLineMapper;
    @Override
    public IPage<MkBillChange> queryMkBillChangePage(Page<MkBillChange> page, MkBillChange mkBillChange) {
//       Page<MkBillChange> mkBillChangePage =  super.baseMapper.selectPage(page, new LambdaQueryWrapper<MkBillChange>()
//               .eq(MkBillChange::getWithdrawCashStatus, mkBillChange.getWithdrawCashStatus())
//               .eq(MkBillChange::getType, mkBillChange.getType())
//       );
      IPage<MkBillChange> mkBillChangePage = super.baseMapper.findBillChangeForPage(page, mkBillChange);
       if(mkBillChangePage.getRecords().size() > 0) {
           this.setMkBillLineVoList(mkBillChangePage.getRecords(),mkBillChange.getType(),mkBillChange.getWithdrawCashStatus());
       }

        return mkBillChangePage;
    }

    /**
     * 处理提现详情vo分站提现记录（集）集合
     */
    private void setMkBillLineVoList(List<MkBillChange> mkBillChangeList,Integer type,Integer withdrawCashStatus) {
        MkBillLine mkBillLine1 = new MkBillLine();
        for (MkBillChange mkBillChange : mkBillChangeList) {
//            List<MkBillLine> mkBillLineList = mkBillLineMapper.selectList(new LambdaQueryWrapper<MkBillLine>()
//                    .eq(MkBillLine::getBillChangeId, mkBillChange.getId())
//                    .eq(MkBillLine::getType, type)
//            );

            mkBillLine1.setType(type);
            mkBillLine1.setBillChangeId(mkBillChange.getId());
            mkBillLine1.setWithdrawCashStatus(withdrawCashStatus);
            List<MkBillLine> mkBillLineList = mkBillLineMapper.findByChange(mkBillLine1);
            //mkBillChangeList.set(mkBillChangeList.indexOf(mkBillChange))
            List<MkBillLineVo> mkBillLineVos = mkBillLineList.stream().map(mk -> {
                MkBillLineVo mkBilllinevo = new MkBillLineVo(mk);
                return mkBilllinevo;
            }).collect(Collectors.toList());
            mkBillChange.setMkBillLineVo(mkBillLineVos);

        }
    }


}
